package com.itheima.Day15;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class XmlParse {
    public static void main(String[] args) throws DocumentException {
        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File("G:\\heina Day 1\\misao\\src\\com\\itheima\\Day15\\a.xml"));
        Element rootElement = document.getRootElement();
        List<Element> studentElements = rootElement.elements("student");
        ArrayList<Student> list = new ArrayList<>();
        for (Element element : studentElements) {
            Attribute attribute = element.attribute("id");
            String id = attribute.getValue();
            Element element1 = element.element("name");
            String name = element1.getText();
            Element ageElement = element.element("age");
            String age = ageElement.getText();
            Student s = new Student(id, name, Integer.parseInt(age));
            list.add(s);
        }
        for (Student student : list) {
            System.out.println(student);
        }
    }
}
